<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Product_category_Model extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    function getAllData($num,$offset){
        $query = $this->db->get('product_category',$num,$offset);
      
        return $query->result();  
    }
    function getCategoryLevel($parent_id=0){
        $query = $this->db->get('product_category',array("parent_id"=>$parent_id));
        return $query->result();  
    }  
	function getSubCategory($product_category_id=0){
       	$this->db->where("parent_id",$product_category_id);
		$this->db->order_by('nb_order','ASC');
		$query = $this->db->get("product_category");
        return $query->result();  
    }  
    function getNumData(){
        $query = $this->db->get('product_category');
        return $query->num_rows();
    }
     function get_object($id)
    {
        $query = $this->db->get_where('product_category', array('product_category_id' =>$id));
        return $query->row();
    }
    function delete($id){
          $this->db->where('product_category_id',$id);
          $query = $this->db->delete('product_category');
          if($query){
              return true;
          }else{
              return false;
          }
    }
	function deleted($id){
          $this->db->where('product_category_id',$id);
		  $data=array('bl_active'=>'-1');
          $query = $this->db->update('product_category',$data);
          if($query){
              return true;
          }else{
              return false;
          }
    }
    function getParentName($product_category_id=0) {
        $this->db->select("name");
        $this->db->where('product_category_id',$product_category_id);
        $query = $this->db->get("product_category");
        return $query->result();
    }
    function get_list_permit(){
        return $this->db->get('permission')->result();
    }
   function optionCategory($value=0){
       $query = $this->db->get('product_category');
       $category_arr = $query->result();
       $str_opt = "";
       $str_opt = "<option value=''> Danh mục gốc</option>";
       foreach($category_arr as $item_opt){
            $str_opt .="<option value='".$item_opt->product_category_id."'".(($value==$item_opt->product_category_id)?" selected":"")."> ".$item_opt->name."</option>";
        }
        return $str_opt;
   }
  
    function optionCategoryLevel($cid = 0, $value = '', $level = 4, $pace = "|"){
        $str = '&nbsp;';
        $query =  $this->db->get_where('product_category',"parent_id = $cid AND bl_active!=-1 ");
        $data= $query->result();
        if(count($data)>0 && $level>0):
         --$level;
          $pace .= "----";
         //die();
         foreach($data as $object):
          $str .= "<option value='".$object->product_category_id."'".($object->product_category_id==$value?" selected=\"selected\"":"").">" . $pace . $object->name. "</option>";
          $str .= $this->optionCategoryLevel($object->product_category_id, $value, $level, $pace);
         endforeach;
        endif;
        return $str;
 }    

   function saveData($product_category_id,$data){
             if($product_category_id!=0){
                    $this->db->where('product_category_id',$product_category_id);
                    if($this->db->update('product_category',$data)){
                         return true;
                    }else{
                         return false;
                    }
             }else{
                    if($this->db->insert('product_category',$data)){
                        return true;
                    }else{
                        return false;
                    }
            }
    }
    function get_num_data($arr_search){
            /*Begin search*/
			if(!empty($arr_search)){
				switch($arr_search['field_search'])
				{
						case 'name':
								$this->db->like('name',$arr_search['key_search']);
								break ;
						
				}
			}
            /*End search*/
            $this->db->where('bl_active',1);
             $query = $this->db->get('product_category');
             return $query->num_rows();
    }
    function get_all_data($num,$offset,$arr_search)
    {
            /*Begin search*/
			if(!empty($arr_search)){
				switch($arr_search['field_search'])
				{
						case 'name':
								$this->db->like('name',$arr_search['key_search']);
								break ;
						
				}
			}
            /*End search*/

            $this->db->where('bl_active',1);
			$this->db->where('parent_id',0);
             $this->db->order_by('product_category_id','ASC');
             $query = $this->db->get('product_category',$num,$offset);
             return $query->result();
    }
	function getRootParent($id) {
		  $arrRoot = array();
		  $objectCategorys = $this->get_object($id);
		  if($objectCategorys){
			  $root_id = $objectCategorys->parent_id;
			  if($root_id!=0){
			   $arrRoot = $this->getRootParent($root_id); 
			  }
			  $arrRoot[] = $objectCategorys;
			}
		   return $arrRoot;
 	}
	function getAlias($id){
		$this->db->where('bl_active',1);
		$this->db->where('product_category_id',$id);
        $result = $this->db->get('product_category')->row();
		if($result)
			return $result->name_alias;
		return '';
    }
  }
?>
